package org.cnam.mobileShopping.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.cnam.mobileShopping.bean.Productlist;
import org.cnam.mobileShopping.sql.DbAdapter;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ProductlistDAO extends DAO<Productlist> {

	private static final String tableName = "productlist";

	/**
	 * Constructeur par défaut lançant la création de toutes les connexions
	 */
	public ProductlistDAO(Context context) {
		sqlHelper = new DbAdapter(context);
	}

	@Override
	public List<Productlist> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void synchronise(Date lastupdate, Integer userId, Context context) {
		// TODO Auto-generated method stub

	}

	@Override
	public long create(Productlist obj) {
		// récupération d'un produit dans la base de données locale
		SQLiteDatabase db = sqlHelper.getWritableDatabase();

		ContentValues cv = new ContentValues();
		// cv.put("productlistid", obj.getProductlistid());
		cv.put("productid", obj.getProductid());
		cv.put("mlistid", obj.getMlistid());
		cv.put("quantity", obj.getQuantity());
		cv.put("statusid", obj.getStatusid());
		cv.put("userid", obj.getUserid());
		cv.put("commonid", obj.getCommonid());
		cv.put("version", obj.getVersion());
		cv.put("synchr", obj.isSynchr() ? 1 : 0);
		long toReturn = db.insert(tableName, "", cv);

		db.close();
		return toReturn;
	}

	@Override
	public boolean delete(Productlist obj) {
		SQLiteDatabase db = sqlHelper.getReadableDatabase();
		db.delete(tableName, "productlistid=?", new String[] { obj
				.getProductlistid().toString() });
		db.close();
		return true;
	}

	@Override
	public boolean update(Productlist obj) {
		SQLiteDatabase db = sqlHelper.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put("productlistid", obj.getProductlistid());
		cv.put("productid", obj.getProductid());
		cv.put("mlistid", obj.getMlistid());
		cv.put("quantity", obj.getQuantity());
		cv.put("statusid", obj.getStatusid());
		cv.put("userid", obj.getUserid());
		cv.put("version", obj.getVersion());
		cv.put("synchr", obj.isSynchr() ? 1 : 0);
		db.update(tableName, cv, "productlistid=?", new String[] { obj
				.getProductlistid().toString() });
		db.close();
		return true;
	}

	@Override
	public Productlist find(int id) {
		Productlist toReturn = null;
		SQLiteDatabase db = sqlHelper.getReadableDatabase();

		Cursor cursor = db.query(tableName, new String[] { "productlistid",
				"productid", "mlistid", "quantity", "statusid", "userid",
				"commonid", "version", "synchr" }, "productlistid=?",
				new String[] { String.valueOf(id) }, null, null, null);
		if (cursor.moveToFirst()) {
			do {
				toReturn = new Productlist();
				toReturn.setProductlistid(cursor.getInt(0));
				toReturn.setProductid(cursor.getInt(1));
				toReturn.setMlistid(cursor.getInt(2));
				toReturn.setQuantity(cursor.getInt(3));
				toReturn.setStatusid(cursor.getInt(4));
				toReturn.setUserid(cursor.getInt(5));
				toReturn.setCommonid(cursor.getString(6));
				toReturn.setVersion(cursor.getInt(7));
				toReturn.setSynchr(cursor.getInt(8) == 1 ? true : false);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		db.close();
		return toReturn;
	}

	/**
	 * Récupération de tous les produits associés à la liste
	 * 
	 * @param mlistid
	 *            La liste à loader
	 * @return Les prpduits
	 */
	public List<Productlist> findByList(Integer mlistid) {
		List<Productlist> toReturn = new ArrayList<Productlist>();
		// envoit du produit à la base de données distante
		// TODO Send to the web service
		// récupération d'un produit dans la base de données locale
		SQLiteDatabase db = sqlHelper.getReadableDatabase();

		// productlistid ,productid int
		// references product,mlistid int references mList,quantity int NOT
		// NULL,statusid int,userid
		Cursor cursor = db.query(tableName, new String[] { "productlistid",
				"productid", "mlistid", "quantity", "statusid", "userid",
				"commonid", "version", "synchr" }, "mlistid=?",
				new String[] { mlistid.toString() }, null, null, null);
		if (cursor.moveToFirst()) {
			do {
				Productlist p = new Productlist();
				p.setProductlistid(cursor.getInt(0));
				p.setProductid(cursor.getInt(1));
				p.setMlistid(cursor.getInt(2));
				p.setQuantity(cursor.getInt(3));
				p.setStatusid(cursor.getInt(4));
				p.setUserid(cursor.getInt(5));
				p.setCommonid(cursor.getString(6));
				p.setVersion(cursor.getInt(7));
				p.setSynchr(cursor.getInt(8) == 1 ? true : false);
				toReturn.add(p);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		db.close();
		return toReturn;
	}

	@Override
	public Productlist findByCommonId(String commonid) {
		Productlist toReturn = null;
		SQLiteDatabase db = sqlHelper.getReadableDatabase();

		Cursor cursor = db.query(tableName, new String[] { "productlistid",
				"productid", "mlistid", "quantity", "statusid", "userid",
				"commonid", "version", "synchr" }, "commonid=?",
				new String[] { String.valueOf(commonid) }, null, null, null);
		if (cursor.moveToFirst()) {
			do {
				toReturn = new Productlist();
				toReturn.setProductlistid(cursor.getInt(0));
				toReturn.setProductid(cursor.getInt(1));
				toReturn.setMlistid(cursor.getInt(2));
				toReturn.setQuantity(cursor.getInt(3));
				toReturn.setStatusid(cursor.getInt(4));
				toReturn.setUserid(cursor.getInt(5));
				toReturn.setCommonid(cursor.getString(6));
				toReturn.setVersion(cursor.getInt(7));
				toReturn.setSynchr(cursor.getInt(8) == 1 ? true : false);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		db.close();
		return toReturn;
	}

}
